/*
 * @Description:
 * @Version: 2.0
 * @Autor: snzo.chen
 * @Date: 2022-03-23 18:49:44
 * @LastEditors: Chen
 * @LastEditTime: 2022-04-25 19:09:23
 */
const Util = {
  jsonSort: (jsonObj: any) => {
    const arr = [];
    for (const key in jsonObj) {
      if (Object.prototype.hasOwnProperty.call(jsonObj, key)) {
        if (jsonObj[key]) {
          arr.push(key);
        }
      }
    }
    arr.sort();
    let str = '';
    for (const key in arr) {
      if (Object.prototype.hasOwnProperty.call(arr, key)) {
        const element = arr[key];
        str += element + '-' + jsonObj[element] + '|';
      }
    }
    return str.substring(0, str.length - 1);
  },
  trimNulls: (jsonObj: any) => {
    for (const key in jsonObj) {
      if (Object.prototype.hasOwnProperty.call(jsonObj, key)) {
        const element = jsonObj[key];
        if (element === 'null' || element === null || element === ''
          || typeof element === 'undefined' || (element instanceof Object && Object.keys(element).length === 0)) {
          delete (jsonObj[key]);
        }else{
          jsonObj[key] = element.toString().replace(/\s*/g,"");
        }
      }
    }
    return jsonObj;
  },
  formatSearchParams: (jsonObj: any) => {
    const body = new URLSearchParams();
    for (const key in jsonObj) {
      if (Object.prototype.hasOwnProperty.call(jsonObj, key)) {
        const element = jsonObj[key];
        body.set(key, element);
      }
    }
    return body;
  },
  isMobile: () => {
    const flag = navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|XiaoMi|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    );
    return flag;
  },
};

export default Util;
